package com.web.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.dao.EnterpriseMapper;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.utils.GetLocation;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class EnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;

    /**
     * 企业详情
     * @param id
     * @return
     */
    public EnterpriseDetails getEnterpriseDetails(Integer id) {
        return  enterpriseMapper.getEnterpriseDetails(id);
    }

    /**
     * 企业设备图片
     * @param id
     * @return
     */
    public String findEnterpriseDevicePhotos(Integer id) {
        return enterpriseMapper.findEnterpriseDevicePhotos(id);
    }

    /**
     * 企业经典案例
     * @param id
     * @return
     */
    public List<Case> getEnterpriseCase(Integer id) {
        return enterpriseMapper.getEnterpriseCase(id);
    }

    /**
     * 企业发布的需求
     * @param id
     * @return
     */
    public List<HistoricalRelease> getEnteriseDemand(Integer id,Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<HistoricalRelease> list = enterpriseMapper.getEnteriseDemand(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i).getId() == null){
                return new ArrayList<>();
            }
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }
    }

    /**
     * 闲置
     * @param id
     * @return
     * @throws IOException
     */
    public List<HistoricalRelease> getEnteriseIdle(Integer id,Integer pageNum) throws IOException {

        PageHelper.startPage(pageNum,10);
        List<HistoricalRelease> list = enterpriseMapper.getEnteriseIdle(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i) == null){
                return new ArrayList<>();
            }
            String[] coordinate = GetLocation.getCoordinate(list.get(i).getAddress());
            String longitude = coordinate[0];//经度
            String latitude = coordinate[1];//纬度
            list.get(i).setLongitude(longitude);
            list.get(i).setLatitude(latitude);
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }

    }

    /**
     * 供应
     * @param id
     * @return
     */
    public Object getEnteriseSupply(Integer id,Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<HistoricalRelease> list = enterpriseMapper.getEnteriseSupply(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i) == null){
                return new ArrayList<>();
            }
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }
    }

    /**
     * 招聘
     * @param id
     * @return
     */
    public List<EnterpriseRecruitment> getEnterpriseRecruitment(Integer id,Integer pageNum) {

        PageHelper.startPage(pageNum,10);
        List<EnterpriseRecruitment> list = enterpriseMapper.getEnterpriseRecruitment(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i) == null){
                return new ArrayList<>();
            }
            list.get(i).setCity(GetLocation.getCityName(GetLocation.getAdd(list.get(i).getLongitude(),list.get(i).getLatitude())));
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }

    }

    /**
     * 动态
     * @param id
     * @return
     */
    public List<HistoricalRelease> getEnterpriseInstantLife(Integer id,Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<HistoricalRelease> list = enterpriseMapper.getEnterpriseInstantLife(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i) == null){
                return new ArrayList<>();
            }
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }
    }

    /**
     * 提问
     * @param id
     * @return
     */
    public List<HistoricalRelease> getEnterpriseQuiz(Integer id,Integer pageNum) {

        PageHelper.startPage(pageNum,10);
        List<HistoricalRelease> list = enterpriseMapper.getEnterpriseQuiz(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i) == null){
                return new ArrayList<>();
            }
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }

    }

    /**
     * 临工
     * @param id
     * @return
     */
    public List<EnterpriseRecruitment> getEnterpriseTemporaryWork(Integer id,Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<EnterpriseRecruitment> list = enterpriseMapper.getEnterpriseTemporaryWork(id);
        for(int i=0;i<list.size();i++){
            if(list.get(i) == null){
                return new ArrayList<>();
            }
        }
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return new ArrayList<>();
        }else{
            return pageInfo.getList();
        }

    }


}
